package com.lingdong.onlinejudge.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingdong.onlinejudge.common.BaseResponse;
import com.lingdong.onlinejudge.common.ErrorCode;
import com.lingdong.onlinejudge.common.ResultUtils;
import com.lingdong.onlinejudge.exception.BusinessException;
import com.lingdong.onlinejudge.model.dto.team.*;
import com.lingdong.onlinejudge.model.entity.Team;
import com.lingdong.onlinejudge.model.vo.team.TeamVo;
import com.lingdong.onlinejudge.service.TeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 队伍
 * @author lzw
 */

@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private TeamService teamService;

    /**
     * 创建队伍
     * @param teamCreateDto 队伍信息
     * @param token token
     * @return 结果
     */
    @PostMapping("/create")
    public BaseResponse<Boolean> createTeam(@RequestBody TeamCreateDto teamCreateDto,
                                            @RequestHeader("token") String token){
        if(teamCreateDto == null || StringUtils.isAnyBlank(token)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = teamService.createTeam(teamCreateDto,token);
        return ResultUtils.success(result);
    }

    /**
     * 获取队伍邀请码
     * @param teamCodeDto 队伍邀请码信息
     * @return 队伍邀请码
     */
    @PostMapping("/code")
    public BaseResponse<String> teamCode(@RequestBody TeamCodeDto teamCodeDto){
        if(teamCodeDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String result = teamService.getTeamCode(teamCodeDto);
        return ResultUtils.success(result);
    }

    /**
     * 获取队伍信息
     * @param id 队伍id
     * @return 队伍信息
     */
    @GetMapping("/get/{id}")
    public BaseResponse<TeamVo> getTeam(@PathVariable Long id){
        if(id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TeamVo result = teamService.getTeam(id);
        return ResultUtils.success(result);
    }

    /**
     * 加入队伍
     * @param teamJoinDto 加入队伍信息
     * @param token token
     * @return 结果
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinDto teamJoinDto,
                                          @RequestHeader("token") String token){
        if(teamJoinDto == null || StringUtils.isAnyBlank(token)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = teamService.joinTeam(teamJoinDto,token);
        return ResultUtils.success(result);
    }

    /**
     * 查询队伍列表
     * @param teamListDto 队伍信息
     * @return 列表
     */
    @PostMapping("/query/list")
    public BaseResponse<List<Team>> queryTeamList(@RequestBody TeamListDto teamListDto){
        if(teamListDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Team> result = teamService.queryTeamList(teamListDto);
        return ResultUtils.success(result);
    }

    /**
     * 分页查询队伍列表
     * @param teamPageDto 分页信息
     * @return 结果
     */
    @PostMapping("/query/page")
    public BaseResponse<Page<Team>> queryTeamByPage(@RequestBody TeamPageDto teamPageDto){
        if(teamPageDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Team> result = teamService.queryTeamByPage(teamPageDto);
        return ResultUtils.success(result);
    }

    /**
     * 解散队伍
     * @param teamId 队伍id
     * @return 结果
     */
    @DeleteMapping("/dissolve")
    public BaseResponse<Boolean> dissolveTeam(@RequestParam Long teamId){
        if(teamId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = teamService.dissolveTeam(teamId);
        return ResultUtils.success(result);
    }

    /**
     * 退出队伍
     * @param teamExitDto 退出队伍信息
     * @return 结果
     */
    @PostMapping("/exit")
    public BaseResponse<Boolean> exitTeam(@RequestBody TeamExitDto teamExitDto){
       if (teamExitDto == null){
           throw new BusinessException(ErrorCode.PARAMS_ERROR);
       }
       Boolean result = teamService.exitTeam(teamExitDto);
       return ResultUtils.success(result);
    }

    /**
     * 审核用户
     * @param teamExamineDto 审核
     * @return 结果
     */
    @PutMapping("/examine")
    public BaseResponse<Boolean> examineTeam(@RequestBody TeamExamineDto teamExamineDto){
        if (teamExamineDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = teamService.examineTeam(teamExamineDto);
        return ResultUtils.success(result);
    }
}
